package com.jhhc.IoUtils;

import com.jhhc.RHDB.Data.Pojo.ST_PPTN_R;
import com.jhhc.RHDB.Data.Pojo.ST_RIVER_R;
import com.jhhc.RHDB.Data.Pojo.ST_RSVR_R;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.sql.*;
import java.util.List;

public class InsertDB {

    private static final String user = "root";

    private static final String pwd = "123456";

    private static final String url = "jdbc:mysql://localhost:3306/aaa";

    private static final String driver = "com.mysql.jdbc.Driver";


    public static Connection getCon() {

        Connection con = null;

        try {

            Class.forName(driver).newInstance();

            con = DriverManager.getConnection(url, user, pwd);

            if (con != null) {

                System.out.println("你已连接到数据库：" + con.getCatalog());

            }

        } catch (Exception e) {

            System.out.println("连接数据库失败！");

            e.printStackTrace();

        }

        return con;

    }


    public boolean insertDB(Object object,String s) {

        Connection con = null;

        Statement stm = null;

        boolean flag = false;


        String sql=null;
        PreparedStatement pst =null;



        try {
            con = getCon();

            stm = con.createStatement();

            // 创建输出文件 result.txt
             pst =con.prepareStatement(sql);



            if (object.equals("ST_PPTN_R")){
                sql = "insert into ST_PPTN_R values(?,?,?,?,?,?,?)";
            }
            if (object.equals("ST_RIVER_R")){
                sql = "insert into ST_RIVER_R values(?,?,?,?,?,?,?,?,?,?,?,?)";
            }
            if (object.equals("ST_RSVR_R")){
                sql = "insert into ST_RSVR_R values(?,?,?,?,?,?,?,?,?,?,?)";
            }

            ResultSet rs = stm.executeQuery(sql);
            while (rs.next()){
                //pst.setString();
            }

            int i = stm.executeUpdate(sql);

            if (i > 0) {

                flag = true;

                System.out.println(flag + "插入数据成功！");

            }

        } catch (Exception e) {

            flag = false;

            e.printStackTrace();

        } finally {

            close(null, stm, con,pst);

        }

        return flag;

    }
    public boolean insertDBPPTN(ST_PPTN_R st_pptn_r) {
        Connection con = null;
        Statement stm = null;
        PreparedStatement pst =null;
        boolean flag = false;
        String sql="insert into ST_PPTN_R values(?,?,?,?,?,?,?)";
        try {
            con = getCon();

            stm = con.createStatement();

            // 创建输出文件 result.txt
           pst =con.prepareStatement(sql);
            ResultSet rs = stm.executeQuery(sql);
            while (rs.next()){
                pst.setString(1,st_pptn_r.getStcd());
                pst.setString(2,st_pptn_r.getTm());
                pst.setDouble(3,st_pptn_r.getDrp());
                pst.setDouble(4,st_pptn_r.getIntv());
                pst.setDouble(5,st_pptn_r.getPdr());
                pst.setDouble(6,st_pptn_r.getDyp());
                pst.setString(7,st_pptn_r.getWth());
            }

            int i = stm.executeUpdate(sql);

            if (i > 0) {

                flag = true;

                System.out.println(flag + "插入数据成功！");

            }

        } catch (Exception e) {

            flag = false;

            e.printStackTrace();

        } finally {

            close(null, stm, con,pst);

        }

        return flag;

    }

    public boolean insertDBPRIVER(ST_RIVER_R st_river_r) {
        Connection con = null;
        Statement stm = null;
        PreparedStatement pst =null;
        boolean flag = false;
        String sql="insert into ST_RIVER_R values(?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            con = getCon();

            stm = con.createStatement();

            // 创建输出文件 result.txt
            pst =con.prepareStatement(sql);
            ResultSet rs = stm.executeQuery(sql);
            while (rs.next()){
                pst.setString(1,st_river_r.getStcd());
                pst.setString(2,st_river_r.getTm());
                pst.setDouble(3,st_river_r.getZ());
                pst.setDouble(4,st_river_r.getQ());
                pst.setBigDecimal(5,st_river_r.getXsa());
                pst.setBigDecimal(6,st_river_r.getXsavv());
                pst.setBigDecimal(7,st_river_r.getXsmxv());
                pst.setString(8,st_river_r.getFlwchrcd());
                pst.setString(9,st_river_r.getWptn());
                pst.setString(10,st_river_r.getMsqmt());
                pst.setString(11,st_river_r.getMsamt());
                pst.setString(12,st_river_r.getMsvmt());
            }

            int i = stm.executeUpdate(sql);

            if (i > 0) {

                flag = true;

                System.out.println(flag + "插入数据成功！");

            }

        } catch (Exception e) {

            flag = false;

            e.printStackTrace();

        } finally {

            close(null, stm, con,pst);

        }

        return flag;

    }

    public boolean insertDBPRSVR(ST_RSVR_R st_rsvr_r) {
        Connection con = null;
        Statement stm = null;
        PreparedStatement pst =null;
        boolean flag = false;
        String sql="insert into ST_RSVR_R values(?,?,?,?,?,?,?,?,?,?,?)";
        try {
            con = getCon();

            stm = con.createStatement();

            // 创建输出文件 result.txt
            pst =con.prepareStatement(sql);
            ResultSet rs = stm.executeQuery(sql);
            while (rs.next()){
                pst.setString(1,st_rsvr_r.getStcd());
                pst.setString(2,st_rsvr_r.getTm());
                pst.setDouble(3,st_rsvr_r.getRz());
                pst.setDouble(4,st_rsvr_r.getInq());
                pst.setDouble(5,st_rsvr_r.getW());
                pst.setDouble(6,st_rsvr_r.getBlrz());
                pst.setDouble(7,st_rsvr_r.getOtq());
                pst.setString(8,st_rsvr_r.getRwchrcd());
                pst.setString(9,st_rsvr_r.getRwptn());
                pst.setDouble(10,st_rsvr_r.getInqdr());
                pst.setString(11,st_rsvr_r.getMsqmt());


            }

            int i = stm.executeUpdate(sql);

            if (i > 0) {

                flag = true;

                System.out.println(flag + "插入数据成功！");

            }

        } catch (Exception e) {

            flag = false;

            e.printStackTrace();

        } finally {

            close(null, stm, con,pst);

        }

        return flag;

    }
    //关闭相关连接

    public void close(ResultSet rs, Statement stm, Connection con,PreparedStatement pst) {

        if (rs != null)

            try {

                rs.close();

            } catch (Exception e) {

                e.printStackTrace();

            }

        if (stm != null)

            try {

                stm.close();

            } catch (Exception e) {

                e.printStackTrace();

            }

        if (con != null)

            try {

                con.close();

            } catch (Exception e) {

                e.printStackTrace();

            }
        if (pst != null)

            try {

                pst.close();

            } catch (Exception e) {

                e.printStackTrace();

            }
    }

}

